//
// PropertyFileConfiguration.cpp
//
// $Id: //poco/1.4/Util/src/PropertyFileConfiguration.cpp#1 $
//
// Library: Util
// Package: Configuration
// Module:  PropertyFileConfiguration
//
// Copyright (c) 2004-2009, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Util/PropertyFileConfiguration.h"
#include "Poco/Exception.h"
#include "Poco/String.h"
#include "Poco/Path.h"
#include <fstream>
#include <set>

using Poco::trim;
using Poco::Path;


namespace Poco {
namespace Util {


PropertyFileConfiguration::PropertyFileConfiguration()
{
}


PropertyFileConfiguration::PropertyFileConfiguration(std::istream& istr)
{
	load(istr);
}

	
PropertyFileConfiguration::PropertyFileConfiguration(const std::string& path)
{
	load(path);
}


PropertyFileConfiguration::~PropertyFileConfiguration()
{
}


void PropertyFileConfiguration::tripchar(std::string& src, char delc)
{
    std:: string tmp;
    int i = 0;
    while( i < src.length())
    {
        if(src[i] == delc)
        {
            ++i;
            continue;
        }
        tmp += src[i];
        ++i;
    }
    src = tmp;
}


void PropertyFileConfiguration::load(std::istream& istr)
{
	m_map.clear();
	const int linelen 		= 256;
	char    buf[linelen]	={'\0'};
	while(!istr.eof())
	{
		memset(buf,'\0',linelen);
		istr.getline(buf,linelen);
		std::string line(buf);
		line = trim(line);
		if(line.empty())
			continue;
		//注释处理
		//std::replace(line.begin(),line.end(),' ','');
		tripchar(line,' ');
		tripchar(line,'\t');
		tripchar(line,'\n');
		tripchar(line,'\r');
		if(line[0] == '#' || (line[0] == '/' && line[1] == '/'))
			continue;

		std::string     key,value;
		int pos         = line.find("=");
		key				= line.substr(0,pos);
		value           = line.substr(pos+1);
		int notepos     = value.find("#");
		if(notepos != std::string::npos)
			value       = value.substr(0,notepos);
		notepos        = value.find("//");
		if(notepos != std::string::npos)
			value       = value.substr(0,notepos);
		notepos        = value.find(";");
		if(notepos != std::string::npos)
			value       = value.substr(0,notepos);
		
		value			= trim(value);
		if (value.size() > 2 && value[0]=='\"' && value[value.size()-1] == '\"')
			value = value.substr(1,value.size()-2);
		
		m_map[trim(key)] = trim(value);
	}  
}

	
void PropertyFileConfiguration::load(const std::string& path)
{
	std::fstream fs(path.c_str());
	load(fs);
	fs.close();
}


void PropertyFileConfiguration::save(std::ostream& ostr) const
{
	std::map<std::string,std::string>::const_iterator it = m_map.begin();
	std::map<std::string,std::string>::const_iterator ed = m_map.end();
	while (it != ed)
	{
		ostr << it->first << ": ";
		for (std::string::const_iterator its = it->second.begin(); its != it->second.end(); ++its)
		{
			switch (*its)
			{
			case '\t':
				ostr << "\\t";
				break;
			case '\r':
				ostr << "\\r";
				break;
			case '\n':
				ostr << "\\n";
				break;
			case '\f':
				ostr << "\\f";
				break;
			case '\\':
				ostr << "\\\\";
				break;
			default:
				ostr << *its;
				break;
			}
		}
		ostr << "\n";
		++it;
	}
}


void PropertyFileConfiguration::save(const std::string& path) const
{
	std::fstream fs(path.c_str());
	if (fs.good())
	{
		save(fs);
		fs.flush();
		if (!fs.good()) throw Poco::WriteFileException(path);
	}
	else throw Poco::CreateFileException(path);
	fs.close();
}


bool PropertyFileConfiguration::getRaw(const std::string& key, std::string& value) const
{
	std::map<std::string,std::string>::const_iterator it = m_map.find(key);
	if (it != m_map.end())
	{
		value = it->second;
		return true;
	}
	else return false;
}


void PropertyFileConfiguration::setRaw(const std::string& key, const std::string& value)
{
	m_map[key] = value;
}


void PropertyFileConfiguration::enumerate(const std::string& key, Keys& range) const
{
	std::set<std::string> keys;
	std::string prefix = key;
	if (!prefix.empty()) prefix += '.';
	std::string::size_type psize = prefix.size();
	for (std::map<std::string,std::string>::const_iterator it = m_map.begin(); it != m_map.end(); ++it)
	{
		if (it->first.compare(0, psize, prefix) == 0)
		{
			std::string subKey;
			std::string::size_type end = it->first.find('.', psize);
			if (end == std::string::npos)
				subKey = it->first.substr(psize);
			else
				subKey = it->first.substr(psize, end - psize);
			if (keys.find(subKey) == keys.end())
			{
				range.push_back(subKey);
				keys.insert(subKey);
			}
		}
	}
}


void PropertyFileConfiguration::removeRaw(const std::string& key)
{
	std::string prefix = key;
	if (!prefix.empty()) prefix += '.';
	std::string::size_type psize = prefix.size();
	std::map<std::string,std::string>::iterator it = m_map.begin();
	std::map<std::string,std::string>::iterator itCur;
	while (it != m_map.end())
	{
		itCur = it++;
		if ((itCur->first == key) || (itCur->first.compare(0, psize, prefix) == 0))
		{
			m_map.erase(itCur);
		}
	}
}

} } // namespace Poco::Util
